﻿// BT02.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "BT02.h"
#include <richedit.h>
#include <Shobjidl.h>
#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE hInst;								// current instance
TCHAR szTitle[MAX_LOADSTRING];					// The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];			// the main window class name
HWND listview;
HWND treecontrol;
HWND toolbar;
HWND editcontrol;
HWND groupbox;
HWND textbox;
HWND Rebar;
int root=0;
int IconNam;
int Icon;
int IconNote;
int IconThang;
int flag=1;
HFONT GUIFont;
int count=0;
int indexnote=-1;
TV_INSERTSTRUCT tvinsert;
HTREEITEM Parent;   
HTREEITEM Nam;
TCHAR CurrentNam[20]=L"";
HTREEITEM Thang;
TCHAR CurrentThang[20]=L"";
HTREEITEM Selected;
_settextex Set;
_gettextex Get;
	bool flagSelected=false;
	TV_ITEM tvi1;
	LV_ITEM LvItem;
	HWND window;
BOOL InsertListViewItems(HWND hWndListView,TCHAR title[],TCHAR Time[],TCHAR context[],HTREEITEM temp);
HTREEITEM AddItemToTree(HWND hwndTV, LPTSTR lpszItem, int nLevel,HTREEITEM parent,int index);
// Forward declarations of functions included in this code module:
ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK	About(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK	SaveDialog(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK	OpenDialog(HWND, UINT, WPARAM, LPARAM);
TCHAR Name[1024];
TVITEM node;

int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPTSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
	UNREFERENCED_PARAMETER(hPrevInstance);
	UNREFERENCED_PARAMETER(lpCmdLine);

 	// TODO: Place code here.
	MSG msg;
	HACCEL hAccelTable;

	// Initialize global strings
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	LoadString(hInstance, IDC_BT02, szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance);

	// Perform application initialization:
	if (!InitInstance (hInstance, nCmdShow))
	{
		return FALSE;
	}

	hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_BT02));

	// Main message loop:
	while (GetMessage(&msg, NULL, 0, 0))
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	return (int) msg.wParam;
}



//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX);

	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_BT02));
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= MAKEINTRESOURCE(IDC_BT02);
	wcex.lpszClassName	= szWindowClass;
	wcex.hIconSm		= LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
	
	return RegisterClassEx(&wcex);

}

//
//   FUNCTION: InitInstance(HINSTANCE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//

/// Rebar
void ShowDialog()
{

}
DWORD CALLBACK EditStreamInCallback(DWORD_PTR dwCookie, LPBYTE lpBuff, LONG cb, PLONG pcb)
{
    HANDLE hFile = (HANDLE)dwCookie;  
    if (ReadFile(hFile, lpBuff, cb, (DWORD *)pcb, NULL)) 
    {
        return 0;
    }    
    return -1;
}
DWORD CALLBACK EditStreamOutCallback(DWORD_PTR dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb)
{

    HANDLE hFile = (HANDLE)dwCookie;
    DWORD NumberOfBytesWritten;
	
	if (!WriteFile(hFile, pbBuff, cb, &NumberOfBytesWritten, NULL))
    {
        return 1;
    }
    *pcb = NumberOfBytesWritten;
    return 0;
}
void SaveRichTextToFile(LPCWSTR filename)
{
	HANDLE hFile = CreateFile(filename, GENERIC_WRITE|GENERIC_ALL, 0, NULL,
		OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    if (hFile == INVALID_HANDLE_VALUE)
    {
        //handle errors
    }
    EDITSTREAM es = { 0 };
    es.dwCookie = (DWORD) hFile;
	es.pfnCallback = EditStreamOutCallback; 
	SendMessage(textbox, EM_STREAMOUT, SF_RTF, (LPARAM)&es);
    CloseHandle(hFile);
    if (es.dwError != 0)
    {
        //handle errors
    }
}
void GhiFile(int type,TCHAR path[])
{
	DWORD wmWritten; 
	TCHAR strData[450];
	TCHAR PathFile[450]=L"Note/";
	TCHAR all[100000]=L"";
	TCHAR Namefile[1024]=L"";
	TCHAR temp[1024]=L"";
	TCHAR temp1[1024]=L"";
	if(type==0)
		_tcscat_s(Namefile,L"Note/MainNote.doc");
	else
	{
		_tcscat_s(Namefile,path);
		_tcscat_s(Namefile,L"/MainNote.doc");
	}
	HANDLE hFile = CreateFile(Namefile, GENERIC_WRITE, 0, NULL,
        CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
	CloseHandle(hFile); 
	for(int i=count-1;i>=0;i--)
	{
		for(int j=0;j<3;j++)
		{
			ListView_GetItemText(listview,i,j,strData,450);
			if(j==1 && type==1)
			{	_tcscat_s(temp1,PathFile);				
				_tcscat_s(temp1,strData);
				_tcscat_s(temp1,L".rtf");
				_tcscat_s(temp,path);
				_tcscat_s(temp,L"/");
				_tcscat_s(temp,strData);
				_tcscat_s(temp,L".rtf");
				CopyFile(temp1,temp,FALSE);
				temp[0]='\0';
				temp1[0]='\0';
			}
			_tcscat_s(strData,_T("\n"));
			_tcscat_s(all,strData);
			
		}
	}
	SendMessage(textbox,EM_SETTEXTEX, (WPARAM)&Set,(LPARAM)all);
	SaveRichTextToFile(Namefile);
	
	SendMessage(textbox,EM_SETTEXTEX, (WPARAM)&Set,(LPARAM)L"");
}
void ThemOldNode(TCHAR Year[],TCHAR Month[],TCHAR Time[],TCHAR title[],TCHAR nodename[])
{	
	if(Nam==NULL)
	{
		Nam=AddItemToTree(treecontrol,Year,1,NULL,NULL);
		Thang=AddItemToTree(treecontrol,Month,2,Nam,NULL);
		wcscpy_s(CurrentNam,Year);
		wcscpy_s(CurrentThang,Month);	
	}
	else
	{
		if(_tcscmp(CurrentNam,Year)==0)
		{
			if(_tcscmp(CurrentThang,Month)==0)
			{
				
			}
			else
			{
				Thang=AddItemToTree(treecontrol,Month,2,Nam,NULL);
				wcscpy_s(CurrentThang,Month);
			}
		}
		else
		{
			Nam=AddItemToTree(treecontrol,Year,1,NULL,NULL);
			Thang=AddItemToTree(treecontrol,Month,3,Nam,NULL);
			wcscpy_s(CurrentNam,Year);
			wcscpy_s(CurrentThang,Month);
		}
	}
	HTREEITEM temp= AddItemToTree(treecontrol, nodename, 3,Thang,count);
	//InsertListViewItems(HWND hWndListView,TCHAR title[],TCHAR Time[],TCHAR context[],HTREEITEM temp)
	BOOL know=InsertListViewItems(listview,title,Time,_T(""),temp);
	//Edit_SetText(editcontrol,_T(""));
	//Edit_SetText(textbox,_T(""));
	indexnote=0;
	//TreeView_SortChildren(treecontrol,Thang,TRUE);
	count++;
}
void DocFile(int type,TCHAR path[])
{
	DWORD wmWritten; 
	TCHAR all[100000]=L"";
	TCHAR temp[450];
	TCHAR Namefile[1024]=L"";
	TCHAR temp2[1024]=L"";//new
	TCHAR temp1[1024]=L"";//curren
	if(type==0)
		_tcscat_s(Namefile,L"Note/MainNote.doc");
	else
	{
		_tcscat_s(Namefile,path);
		_tcscat_s(Namefile,L"/MainNote.doc");
	}
	HANDLE hFile = CreateFile(Namefile, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING,FILE_FLAG_SEQUENTIAL_SCAN, NULL);  
	TCHAR col1[450]=L"";
	TCHAR col2[450]=L"";
	TCHAR col3[450]=L"";
	TCHAR nam1[10];
	TCHAR thang1[10];
	TCHAR noname[20]=L"Tháng ";
	TCHAR noname1[20]=L"";
    if (hFile != INVALID_HANDLE_VALUE) 
    {
		CopyFile(Namefile,L"Note/MainNote.doc",FALSE);
        EDITSTREAM es = { 0 };       
        es.pfnCallback = EditStreamInCallback;
        es.dwCookie    = (DWORD_PTR)hFile;        
        if (SendMessage(textbox, EM_STREAMIN, SF_RTF, (LPARAM)&es) && es.dwError == 0) 
        {
               
        }        
        CloseHandle(hFile);
		SendMessage(textbox,EM_GETTEXTEX,(WPARAM)&Get,(LPARAM)all);
		SendMessage(textbox,EM_SETTEXTEX, (WPARAM)&Set,(LPARAM)L"");
    }   
	int j=0;
	int col=1;
	for(int i=0;i<100000;i++)
	{
		if(all[i]!='\0')
		{
			if(all[i]!='\r')
			{	
				temp[j]=all[i];
				j++;
			}
			else
			{
				temp[j]='\0';
							
				switch(col)
				{
				case 1:_tcscat_s(col1,temp);
					break;
				case 2:_tcscat_s(col2,temp);
					break;
				case 3:_tcscat_s(col3,temp);
					col=0;
					if(type==1)
					{
						_tcscat_s(temp1,path);
						_tcscat_s(temp1,L"/");
						_tcscat_s(temp1,col2);
						_tcscat_s(temp1,L".rtf");
						_tcscat_s(temp2,L"Note/");
						_tcscat_s(temp2,col2);
						_tcscat_s(temp2,L".rtf");
						CopyFile(temp1,temp2,FALSE);
					}
					temp1[0]='\0';
					temp2[0]='\0';
					//thang
					thang1[0]=temp[3];
					thang1[1]=temp[4];
					thang1[2]='\0';
					if(thang1[0]=='0')
					{
						thang1[0]=temp[4];
						thang1[1]='\0';
					}
					
					_tcscat_s(noname1,noname);
					_tcscat_s(noname1,thang1);
					//nam
					nam1[0]=temp[6];
					nam1[1]=temp[7];
					nam1[2]=temp[8];
					nam1[3]=temp[9];
					nam1[4]='\0';

					ThemOldNode(nam1,noname1,col3,col2,col1);
					col1[0]='\0';
					col2[0]='\0';
					col3[0]='\0';					
					thang1[0]='\0';	
					nam1[0]='\0';
					noname1[0]='\0';
					break;
				}
				col++;
				temp[0]='\0';
				j=0;	
			}
		}
		else
		{
			break;
		}
	}
}
HWND CreateRebar(HWND hwndOwner, HWND hwndToolbar)
{
    // Check parameters.
    if ((hwndToolbar == NULL) )
    {
        return NULL;
    }

    // Initialize common controls.
    INITCOMMONCONTROLSEX icex;
    icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
    icex.dwICC   = ICC_COOL_CLASSES | ICC_BAR_CLASSES;
    InitCommonControlsEx(&icex);

    // Create the rebar.
    HWND hwndRebar = CreateWindowEx(WS_EX_TOOLWINDOW,
        REBARCLASSNAME,
        NULL,
        WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS |
        WS_CLIPCHILDREN | RBS_VARHEIGHT |
        CCS_NODIVIDER | RBS_BANDBORDERS|RBS_AUTOSIZE|RBS_FIXEDORDER,
       700, 80, 300, 370,
        hwndOwner,
        NULL,
		hInst, // global instance handle
        NULL);

    if(!hwndRebar)
    {
        return NULL;
    }

    // Initialize band info used by both bands.
    REBARBANDINFO rbBand = { sizeof(REBARBANDINFO) };
    rbBand.fMask  = 
          RBBIM_STYLE       // fStyle is valid.
        | RBBIM_TEXT        // lpText is valid.
        | RBBIM_CHILD       // hwndChild is valid.
        | RBBIM_CHILDSIZE   // child size members are valid.
        | RBBIM_SIZE;       // cx is valid
    rbBand.fStyle = RBBS_CHILDEDGE | RBBS_GRIPPERALWAYS;  

    // Get the height of the toolbar.
    DWORD dwBtnSize = (DWORD)SendMessage(hwndToolbar, TB_GETBUTTONSIZE, 0,0);

    // Set values unique to the band with the toolbar.
    rbBand.lpText = TEXT("");
    rbBand.hwndChild = hwndToolbar;
    rbBand.cyChild = LOWORD(dwBtnSize);
    rbBand.cxMinChild = 3 * HIWORD(dwBtnSize);
    rbBand.cyMinChild = LOWORD(dwBtnSize);
    // The default width is the width of the buttons.
    rbBand.cx = 0;

    // Add the band that has the toolbar.
    SendMessage(hwndRebar, RB_INSERTBAND, (WPARAM)-1, (LPARAM)&rbBand);

    // Set values unique to the band with the combo box.
   
    
    return (hwndRebar);
}

/////
HTREEITEM AddItemToTree(HWND hwndTV, LPTSTR lpszItem, int nLevel,HTREEITEM parent,int index)
{ 
    TVITEM tvi; 
    TVINSERTSTRUCT tvins; 
    static HTREEITEM hPrev = (HTREEITEM)TVI_FIRST; 
    static HTREEITEM hPrevRootItem = NULL; 
    static HTREEITEM hPrevLev2Item = NULL; 
    HTREEITEM hti; 

    tvi.mask = TVIF_TEXT | TVIF_IMAGE 
               | TVIF_SELECTEDIMAGE | TVIF_PARAM; 

   // Set the text of the item. 
    tvi.pszText = lpszItem; 
    tvi.cchTextMax = sizeof(tvi.pszText)/sizeof(tvi.pszText[0]); 
	
	switch (nLevel)
	{
	case 1:
		tvi.iImage =IconNote; 
		tvi.iSelectedImage =IconNote;
		break;
	case 2:
		tvi.iImage =IconThang; 
		tvi.iSelectedImage =IconThang;
		break;
	case 3:
		tvi.iImage =IconNam;
		tvi.iSelectedImage =IconNam;
		break;
	}
	//tvi.iSelectedImage = NULL; 
	tvi.lParam = index; 
    tvins.item = tvi; 
    tvins.hInsertAfter = hPrev; 
    // Set the parent item based on the specified level. 
    if (nLevel == 1) 
        tvins.hParent = TVI_ROOT; 
    else if (nLevel == 2) 
		tvins.hParent = parent; 
    else 
        tvins.hParent = parent; 
    // Add the item to the tree-view control. 
    hPrev = (HTREEITEM)SendMessage(hwndTV, TVM_INSERTITEM, 
        0, (LPARAM)(LPTVINSERTSTRUCT)&tvins); 
    if (hPrev == NULL)
        return NULL;
    // Save the handle to the item. 
    if (nLevel == 1) 
        hPrevRootItem = hPrev; 
    else if (nLevel == 2) 
        hPrevLev2Item = hPrev; 
	else if (nLevel == 3) 
        hPrevLev2Item = hPrev;
    // The new item is a child item. Give the parent item a 
    // closed folder bitmap to indicate it now has child items. 
    
    return hPrev; 
} 
BOOL InitTreeViewImageLists(HWND hwndTV) 
{ 
    HIMAGELIST himl;  // handle to image list 
    HBITMAP hbmp;     // handle to bitmap 

    // Create the image list. 
    if ((himl = ImageList_Create(21, 
                                 21,
                                 FALSE, 
                                4, 0)) == NULL) 
        return FALSE; 

    // Add the open file, closed file, and IconThang bitmaps. 
	hbmp = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP3)); 
    IconThang = ImageList_Add(himl, hbmp, (HBITMAP)NULL); 
    DeleteObject(hbmp); 

    hbmp= LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP4)); 
    IconNote = ImageList_Add(himl, hbmp, (HBITMAP)NULL); 
    DeleteObject(hbmp); 

    hbmp = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP6)); 
    IconNam = ImageList_Add(himl, hbmp, (HBITMAP)NULL); 
    DeleteObject(hbmp); 

	hbmp = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP5)); 
    Icon = ImageList_Add(himl, hbmp, (HBITMAP)NULL); 
    DeleteObject(hbmp); 

    // Associate the image list with the tree-view control. 
    TreeView_SetImageList(hwndTV, himl, TVSIL_NORMAL); 

    return TRUE; 
} 

/////////////////////////////////////////////////////////
/// Tool Bar
//////////////////////////////////////////////////////////

HWND CreateSimpleToolbar(HWND hWndParent)
{
    // Declare and initialize local constants.
    const int ImageListID    = 0;
    const int numButtons     = 5;
    const int bitmapSize     = 16;
    HIMAGELIST g_hImageList = NULL;
    const DWORD buttonStyles = BTNS_AUTOSIZE;
    // Create the toolbar.
    HWND hWndToolbar = CreateWindowEx(0, TOOLBARCLASSNAME, NULL, 
		WS_CHILD |CCS_NOPARENTALIGN, 700,30,300,50, hWndParent, NULL, hInst, NULL);     
    if (hWndToolbar == NULL)
        return NULL;
    // Create the image list.
    g_hImageList = ImageList_Create(bitmapSize, bitmapSize,   // Dimensions of individual bitmaps.
                                    ILC_COLOR16 | ILC_MASK,   // Ensures transparent background.
                                    numButtons, 0);
    // Set the image list.
    SendMessage(hWndToolbar, TB_SETIMAGELIST, (WPARAM)ImageListID, (LPARAM)g_hImageList);
    // Load the button images.
    SendMessage(hWndToolbar, TB_LOADIMAGES, (WPARAM)IDB_STD_SMALL_COLOR, (LPARAM)HINST_COMMCTRL);
    // Initialize button info.
    // IDM_NEW, IDM_OPEN, and IDM_SAVE are application-defined command constants.   
	SendMessage(toolbar,TB_SETBITMAPSIZE,0,MAKELONG(29,29));
	TBADDBITMAP tbAddBitmap;
	tbAddBitmap.hInst=hInst;
	tbAddBitmap.nID=IDB_BITMAP7;
	SendMessage(toolbar,TB_ADDBITMAP,3,(LPARAM)&tbAddBitmap);
	
    TBBUTTON tbButtons[numButtons] = 
    {
		{ IDB_BITMAP7, ID_BOLD,  TBSTATE_ENABLED, buttonStyles|BTNS_CHECK, {0}, 0, (INT_PTR)L"B" },
		{ IDB_BITMAP7, ID_ITALIC, TBSTATE_ENABLED, buttonStyles|BTNS_CHECK, {0}, 0, (INT_PTR)L"I"},
		{IDB_BITMAP7, ID_UNDER, TBSTATE_ENABLED, buttonStyles|BTNS_CHECK, {0}, 0, (INT_PTR)L"U"},
		{ IDB_BITMAP7, NULL, TBSTATE_ENABLED, buttonStyles|BTNS_SEP, {0}, 0, (INT_PTR)L"UNDERLINE"},
		{ MAKELONG(STD_FILESAVE, ImageListID), ID_SAVE, TBSTATE_ENABLED, buttonStyles, {0}, 0, (INT_PTR)L"Save"}
    };
    // Add buttons.
    SendMessage(hWndToolbar, TB_BUTTONSTRUCTSIZE, (WPARAM)sizeof(TBBUTTON), 0);
    SendMessage(hWndToolbar, TB_ADDBUTTONS,       (WPARAM)numButtons,       (LPARAM)&tbButtons);
    // Resize the toolbar, and then show it.
    SendMessage(hWndToolbar, TB_AUTOSIZE, 0, 0); 
    ShowWindow(hWndToolbar,  TRUE);
    
    return hWndToolbar;
}

////////////////////////////////////////////////////////////
// List view
BOOL InitListViewImageLists(HWND hWndListView) 
{ 
   HICON hiconItem;     // Icon for list-view items.
    HIMAGELIST hSmall;   // Image list for other views.
    // Create the full-sized icon image lists. 
   hSmall = ImageList_Create(GetSystemMetrics(SM_CXSMICON), 
                              GetSystemMetrics(SM_CYSMICON), 
                              ILC_MASK, 1, 1);    
    // Add an icon to each image list.  
   hiconItem = LoadIcon(hInst, MAKEINTRESOURCE(IDI_ICON1));
    ImageList_AddIcon(hSmall, hiconItem);
    DestroyIcon(hiconItem);
    // Assign the image lists to the list-view control. 
	ListView_SetImageList(hWndListView, hSmall, LVSIL_SMALL);

	
    return TRUE;
}
BOOL InsertListViewItems(HWND hWndListView,TCHAR title[],TCHAR Time[],TCHAR context[],HTREEITEM temp)
{
	Selected=temp;
	TCHAR MainTitte[1024]=L"";
	wcscat_s(MainTitte,Time);
	wcscat_s(MainTitte,_T(" - "));
	wcscat_s(MainTitte,title);
    LVITEM lvI;
	HIMAGELIST ab=ListView_GetImageList(listview,LVSIL_SMALL);
	int index=0;
	lvI.pszText   = MainTitte; // Sends an LVN_GETDISPINFO message.		
	lvI.mask      = LVIF_TEXT | LVIF_IMAGE |LVIF_STATE| LVIF_PARAM ;
	lvI.stateMask = 0;
	lvI.iSubItem  = 0;
	lvI.state     = 0;
	lvI.iItem  = Icon;
    lvI.iImage = index;
	lvI.lParam = (LPARAM)temp;
    if (ListView_InsertItem(hWndListView, &lvI) == -1)
        return FALSE;
	for(int i=1;i<4;i++)
	{
		if(i==1)
			ListView_SetItemText(listview,index,i,title);
		if(i==2)
			ListView_SetItemText(listview,index,i,Time);
		if(i==3)
			ListView_SetItemText(listview,index,i,context);
	}
	
    return TRUE;
}
HWND CreateListView (HWND hwndParent) 
{
    INITCOMMONCONTROLSEX icex;           // Structure for control initialization.
    icex.dwICC = ICC_LISTVIEW_CLASSES ;
    InitCommonControlsEx(&icex);
    // Create the list-view window in report view with label editing enabled.
    HWND hWndListView = CreateWindow(WC_LISTVIEW,  L"",
		WS_CHILD | LVS_REPORT | WS_VISIBLE | WS_BORDER|LVS_SHOWSELALWAYS|LVS_SORTDESCENDING|LVS_SINGLESEL,
    250, 0,450, 450,hwndParent,(HMENU)ID_LISTVIEW,hInst,NULL); 
    return (hWndListView);
}
BOOL InitListViewColumns(HWND hWndListView) 
{ 
    WCHAR szText[256];     // Temporary buffer.
    LVCOLUMN lvc;
    int iCol;
	BOOL know;
    lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
    for (iCol = 0; iCol < 4; iCol++)
    {
		TCHAR s[4];
		_itot_s(iCol + 1,s,10);
		if(iCol==0)
			_tcscpy_s(szText, sizeof(szText)/sizeof(TCHAR),  _T("Tiêu Đề + Ngày Tháng"));
		if(iCol==1)
			_tcscpy_s(szText, sizeof(szText)/sizeof(TCHAR),  _T("Tiêu Đề"));
		if(iCol==2)
			_tcscpy_s(szText, sizeof(szText)/sizeof(TCHAR),  _T("Ngày Tháng"));
		if(iCol==3)
			_tcscpy_s(szText, sizeof(szText)/sizeof(TCHAR),  _T("Nội Dung"));
        lvc.iSubItem = iCol;
        lvc.pszText = szText;
        lvc.cx = 166;               // Width of column in pixels.
        lvc.fmt = LVCFMT_LEFT;  // Left-aligned column.
        if (ListView_InsertColumn(hWndListView, iCol, &lvc) == -1)
            return FALSE;
    }
	BOOL a=InitListViewImageLists(listview) ;
    return TRUE;
} 

//////////////////////////////////////
void InitControl(HWND hWnd)
{ 
	
	InitCommonControls(); 
	listview=CreateListView(hWnd);
	SetWindowFont(listview, GUIFont, TRUE); 
	treecontrol=CreateWindowEx(0,WC_TREEVIEW,_T("Tree View"), TVS_LINESATROOT|TVS_HASBUTTONS|WS_VISIBLE | WS_CHILD | WS_BORDER | TVS_HASLINES|TVS_SHOWSELALWAYS
		,0,0,250,450,hWnd,(HMENU)ID_TREEVIEW,hInst,NULL);
	SetWindowFont(treecontrol, GUIFont, TRUE); 
	editcontrol=CreateWindow(_T("EDIT"),_T(""),ES_LEFT|WS_VISIBLE | WS_CHILD | WS_BORDER| WS_VSCROLL|ES_AUTOHSCROLL,700,0,300,30,hWnd,(HMENU)ID_EDITCONTROL,hInst,NULL);
	//toolbar=CreateSimpleToolbar(hWnd);
	SetWindowFont(editcontrol, GUIFont, TRUE); 
/*	textbox= CreateWindowEx(
                                0, L"EDIT",   // pIconNoteefined class 
                                NULL,         // no window title 
                                WS_CHILD | WS_VISIBLE | WS_VSCROLL | 
								ES_LEFT | ES_MULTILINE | ES_AUTOVSCROLL|WS_BORDER, 
                                700, 80, 300, 370,   // set size in WM_SIZE message 
                                hWnd,         // parent window 
                                (HMENU) ID_TEXTBOX,   // edit control ID 
                                (HINSTANCE) GetWindowLong(hWnd, GWL_HINSTANCE), 
                                NULL);*/
	//LoadLibrary(TEXT("Msftedit.dll"));
	HMODULE aa= LoadLibrary(TEXT("Msftedit.dll"));
   if(NULL == aa)
   {
      MessageBox(hWnd,L"NO DLL",L"Error",MB_OK|MB_ICONINFORMATION);
      return;
   }
	textbox=CreateWindowEx(0, MSFTEDIT_CLASS, TEXT(""),
        ES_MULTILINE | WS_VISIBLE | WS_CHILD | WS_BORDER | WS_TABSTOP,
        700, 80, 300, 370,
		hWnd, (HMENU) ID_TEXTBOX, hInst, NULL);
	SetWindowFont(textbox, GUIFont, TRUE); 
	BOOL a=InitTreeViewImageLists(treecontrol);
	a=InitListViewColumns(listview);
	//groupbox=CreateWindow(_T("BUTTON"),_T(""),WS_CHILD|WS_VISIBLE|BS_GROUPBOX,700,30,300,50,hWnd,NULL,hInst,NULL);
	toolbar=CreateSimpleToolbar(hWnd);
	//Rebar=CreateRebar(hWnd,toolbar);
	///
	Get.cb=1024;
	Get.flags=GT_DEFAULT;
	Get.codepage=1200;
	Get.lpDefaultChar=NULL;
	Get.lpUsedDefChar=NULL;

	Set.flags=ST_DEFAULT;
	Set.codepage=1200;
	SendMessage(textbox,EM_SETEVENTMASK,0,ENM_CHANGE|EN_SETFOCUS|EN_KILLFOCUS);
	DocFile(0,NULL);
}
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   hInst = hInstance; // Store instance handle in our global variable
   GUIFont = (HFONT)GetStockObject( DEFAULT_GUI_FONT );
   
   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
  InitControl(hWnd);
   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND	- process the application menu
//  WM_PAINT	- Paint the main window
//  WM_DESTROY	- post a quit message and return
//
//


void ThemNote()
{	
	TCHAR Time[150];
	TCHAR Year[20]=_T("");
	TCHAR Month[20]=_T("");
	TCHAR Hour[5];
	TCHAR Minute[5];
	TCHAR Second[5];
	TCHAR Milisecond[5];
	TCHAR title[450];
	TCHAR context[4024];
	
	 SYSTEMTIME lt; 
	GetLocalTime(&lt);
	 GetDateFormat
	  (
		LOCALE_USER_DEFAULT,
		0,
		&lt,
		L"dd/MM/yyyy",
		Time,
		256
	  );
	 _itot_s(lt.wYear,Year,10);

	 _itot_s(lt.wMonth,Hour,10);
	 _tcscat_s(Month,_T("Tháng ")); 
	 _tcscat_s(Month,Hour);

	_itot_s(lt.wHour,Hour,10);
	_itot_s(lt.wMinute,Minute,10);
	_itot_s(lt.wSecond,Second,10);
	_itot_s(lt.wMilliseconds,Milisecond,10);
	_tcscat_s(Time,_T(" ")); _tcscat_s(Time,Hour); 
	_tcscat_s(Time,_T(":")); _tcscat_s(Time,Minute); 
	_tcscat_s(Time,_T(":")); _tcscat_s(Time,Second); 
	_tcscat_s(Time,_T(":")); _tcscat_s(Time,Milisecond);


	if(Nam==NULL)
	{
		Nam=AddItemToTree(treecontrol,Year,1,NULL,NULL);
		Thang=AddItemToTree(treecontrol,Month,2,Nam,NULL);
		wcscpy_s(CurrentNam,Year);
		wcscpy_s(CurrentThang,Month);	
	}
	else
	{
		if(_tcscmp(CurrentNam,Year)==0)
		{
			if(_tcscmp(CurrentThang,Month)==0)
			{
				
			}
			else
			{
				Thang=AddItemToTree(treecontrol,Month,2,Nam,NULL);
				wcscpy_s(CurrentThang,Month);
			}
		}
		else
		{
			Nam=AddItemToTree(treecontrol,Year,1,NULL,NULL);
			Thang=AddItemToTree(treecontrol,Month,3,Nam,NULL);
			wcscpy_s(CurrentNam,Year);
			wcscpy_s(CurrentThang,Month);
		}
	}
	HTREEITEM temp= AddItemToTree(treecontrol, _T(""), 3,Thang,count);
	
	BOOL know=InsertListViewItems(listview,_T(""),Time,_T(""),temp);
	//Edit_SetText(editcontrol,_T(""));
	//Edit_SetText(textbox,_T(""));
	indexnote=0;
	//TreeView_SortChildren(treecontrol,Thang,TRUE);
	count++;
}
int focus=-1;
void updatenode()
{
	for(int i=0;i<count;i++)
	{	
		TCHAR text[650];
		memset(&LvItem,0,sizeof(LvItem));
		LvItem.mask=LVIF_TEXT | LVIF_IMAGE |LVIF_STATE| LVIF_PARAM;
		LvItem.iSubItem=0;				  
		LvItem.cchTextMax=450;
		LvItem.iItem=i;
		LvItem.pszText=text;
		ListView_GetItem(listview,&LvItem);
		HTREEITEM node=(HTREEITEM)LvItem.lParam;
	    TreeView_SelectItem(treecontrol,node);
		TCHAR Text[255];
		memset(&tvi1,0,sizeof(tvi1));
		TreeView_EnsureVisible(treecontrol,node);
		SendDlgItemMessage(treecontrol,ID_TREEVIEW,TVM_SELECTITEM,TVGN_CARET,(LPARAM)Selected);
		flagSelected=true;
		tvi1.mask=TVIF_TEXT | TVIF_IMAGE 
        | TVIF_SELECTEDIMAGE | TVIF_PARAM;
		tvi1.pszText=LvItem.pszText;
		tvi1.cchTextMax=256;
		tvi1.hItem=node;
		tvi1.lParam=count-i-1;
		tvi1.iImage =IconNam; 
		tvi1.iSelectedImage = IconNote; 
		TreeView_SetItem(treecontrol,&tvi1);
	}
}
void DelNode(HWND hWnd,int type)
{
	TCHAR title[450];
	TCHAR title1[450]=L"Note/";
	
	if(type==1)
	{
		TCHAR Text[450];
		int iSelect=0;
		iSelect=SendMessage(listview,LVM_GETNEXTITEM,-1,LVNI_FOCUSED);
		memset(&LvItem,0,sizeof(LvItem));
		LvItem.mask=LVIF_TEXT | LVIF_IMAGE |LVIF_STATE| LVIF_PARAM;
		LvItem.iSubItem=0;				  
		LvItem.cchTextMax=450;
		LvItem.pszText=Text;
		LvItem.iItem=iSelect;
		
		ListView_GetItem(listview,&LvItem);
		ListView_GetItemText(listview,iSelect,1,title,350);
		int a=MessageBox(hWnd,_T("Bạn có muốn xóa note này ?"),_T("MEssage"),MB_YESNO);
		if(a==6)
		{		
			if(indexnote==iSelect)
			{
				indexnote=-1;
				Edit_SetText(editcontrol,_T(""));
				SendMessage(textbox,EM_SETTEXTEX, (WPARAM)&Set,(LPARAM)_T(""));
			}
			;
			_tcscat_s(title,L".rtf");
			_tcscat_s(title1,title);
			DeleteFileW(title1);
			ListView_DeleteItem(listview,iSelect);
			TreeView_DeleteItem(treecontrol,(HTREEITEM)LvItem.lParam);
			count--;
			
		}
	}
	else
	{
		TCHAR Text[450];
		memset(&tvi1,0,sizeof(tvi1));
		Selected=(HTREEITEM)SendDlgItemMessage(hWnd,ID_TREEVIEW,TVM_GETNEXTITEM,TVGN_CARET,(LPARAM)Selected);				
		if(Selected==NULL)
		{
				MessageBox(hWnd,_T("TreeView"),_T("Error"),MB_OK|MB_ICONINFORMATION);
				return;
		}				
		TreeView_EnsureVisible(hWnd,Selected);
		SendDlgItemMessage(hWnd,ID_TREEVIEW,TVM_SELECTITEM,TVGN_CARET,(LPARAM)Selected);
		flagSelected=true;
		tvi1.mask=TVIF_TEXT | TVIF_IMAGE 
        | TVIF_SELECTEDIMAGE | TVIF_PARAM;
		tvi1.pszText=Text;
		tvi1.cchTextMax=450;
		tvi1.hItem=Selected;
		TreeView_GetItem(treecontrol,&tvi1);
		int a=MessageBox(hWnd,_T("Bạn có muốn xóa note này ?"),_T("MEssage"),MB_YESNO);
		ListView_GetItemText(listview,count-1-tvi1.lParam,1,title,350);
		if(a==6)
		{				
			if(indexnote==count-1-tvi1.lParam)
			{
				indexnote=-1;
				Edit_SetText(editcontrol,_T(""));
				SendMessage(textbox,EM_SETTEXTEX, (WPARAM)&Set,(LPARAM)_T(""));
			}
			_tcscat_s(title,L".rtf");
			_tcscat_s(title1,title);
			DeleteFileW(title1);
			ListView_DeleteItem(listview,count-1-tvi1.lParam);
			TreeView_DeleteItem(treecontrol,Selected);
			count--;
			updatenode();
		}
	}
	GhiFile(0,NULL);
}


BOOL FillRichEditFromFile(HWND hwnd, LPCTSTR pszFile)
{
    BOOL fSuccess = FALSE;  
    HANDLE hFile = CreateFile(pszFile, GENERIC_READ, 
             FILE_SHARE_READ, 0, OPEN_EXISTING,FILE_FLAG_SEQUENTIAL_SCAN, NULL);        
    if (hFile != INVALID_HANDLE_VALUE) 
    {
        EDITSTREAM es = { 0 };       
        es.pfnCallback = EditStreamInCallback;
        es.dwCookie    = (DWORD_PTR)hFile;        
        if (SendMessage(textbox, EM_STREAMIN, SF_RTF, (LPARAM)&es) && es.dwError == 0) 
        {
                fSuccess = TRUE;
        }        
        CloseHandle(hFile);
    }   
    return fSuccess;   
}
//save


void SaveRichTextToFile(HWND hWnd, LPCWSTR filename)
{
    HANDLE hFile = CreateFile(filename, GENERIC_WRITE, 0, NULL,
        CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
    if (hFile == INVALID_HANDLE_VALUE)
    {
        //handle errors
    }
    EDITSTREAM es = { 0 };
    es.dwCookie = (DWORD) hFile;
	es.pfnCallback = EditStreamOutCallback; 
	SendMessage(textbox, EM_STREAMOUT, SF_RTF, (LPARAM)&es);
    CloseHandle(hFile);
    if (es.dwError != 0)
    {
        //handle errors
    }
}
void SaveText(HWND hwnd,LPCWSTR filename)
{
	TCHAR name[450]=L"Note/";
	_tcscat_s(name,filename);
	_tcscat_s(name,L".rtf");
	SaveRichTextToFile(hwnd,name);	
}
void settext(HWND Hwnd,LPCWSTR filename)
{
	TCHAR name[450]=L"Note/";
	_tcscat_s(name,filename);
	_tcscat_s(name,L".rtf");
	FillRichEditFromFile(Hwnd,name);
}
typedef struct _CHANGENOTIFY {
  DWORD dwChangeType;
  void  *pvCookieData;
} CHANGENOTIFY;
int edit=0;
int text=0;
_charformat bold;
void formattext(int type)
{
	int Bold,Italic,Under;
	
	Bold=SendMessage(toolbar,TB_ISBUTTONCHECKED,ID_BOLD,0);
	Italic=SendMessage(toolbar,TB_ISBUTTONCHECKED,ID_ITALIC,0);
	Under=SendMessage(toolbar,TB_ISBUTTONCHECKED,ID_UNDER,0);	
	//Bold=SendMessage(toolbar,TB_GETSTATE,ID_BOLD,0);
	///MessageBox(hWnd,note,_T("Thông Báo !"),MB_OK|MB_ICONINFORMATION);
	CHARFORMAT cfmB;
	cfmB.cbSize = sizeof(cfmB);    // Don't forget this!
	cfmB.crTextColor = CFE_AUTOCOLOR;
	cfmB.dwMask      = CFM_BOLD | CFM_COLOR |CFM_ITALIC|CFM_UNDERLINE;
						    
		// Set the new effects					
		
		if(Bold!=1 &&Italic!=1 && Under!=1)	//no
			cfmB.dwEffects   = CFM_PROTECTED;
		if(Bold==1 &&Italic!=1 && Under!=1)//B
			cfmB.dwEffects   = CFM_BOLD;
		if(Bold==1 &&Italic==1 && Under!=1)//B I
			cfmB.dwEffects   = CFM_BOLD|CFM_ITALIC;
		if(Bold==1 &&Italic!=1 && Under==1) // B U
			cfmB.dwEffects   = CFM_BOLD|CFM_UNDERLINE;
		if(Bold!=1 &&Italic==1 && Under!=1) // I
			cfmB.dwEffects   = CFM_ITALIC;
		if(Bold!=1 &&Italic!=1 && Under==1) // U
			cfmB.dwEffects   = CFM_UNDERLINE;
		if(Bold==1 &&Italic==1 && Under==1)//B I U
			cfmB.dwEffects   = CFM_BOLD|CFM_UNDERLINE|CFM_ITALIC;
		if(Bold!=1 &&Italic==1 && Under==1)//B I U
			cfmB.dwEffects   = CFM_UNDERLINE|CFM_ITALIC;
		SendMessage(textbox, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cfmB);
}
void Save()
{
	TCHAR title[350];
	ListView_GetItemText(listview,indexnote,1,title,450);
	SaveText(NULL,title);
	indexnote=-1;
	Edit_SetText(editcontrol,_T(""));
	SendMessage(textbox,EM_SETTEXTEX, (WPARAM)&Set,(LPARAM)_T(""));
	MessageBox(textbox,_T("Đã Save"),_T("Thông Báo !"),MB_OK|MB_ICONINFORMATION);
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;
		
	switch (message)
	{
	case WM_NOTIFY:
		
		switch(LOWORD(wParam))
		{
		
			case ID_LISTVIEW:				
							
				if(((LPNMHDR)lParam)->code == NM_DBLCLK)
				{
											  
					  TCHAR title[450];
					  TCHAR context[4024];
					  int iSelect=0;
					  iSelect=SendMessage(listview,LVM_GETNEXTITEM,-1,LVNI_FOCUSED);
					  indexnote=iSelect;
					  if(iSelect==-1)
					  {
						//MessageBox(hWnd,L"No Items in ListView",L"Error",MB_OK|MB_ICONINFORMATION);
						break;
					  }
					  memset(&LvItem,0,sizeof(LvItem));
					  LvItem.mask=LVIF_TEXT | LVIF_IMAGE |LVIF_STATE| LVIF_PARAM;
					  LvItem.iSubItem=0;
					  
					  LvItem.cchTextMax=450;
					  LvItem.iItem=iSelect;
					  ListView_GetItem(listview,&LvItem);

					 // SendMessage(listview,LVM_GETITEMTEXT, iSelect, (LPARAM)&LvItem);
					  ListView_GetItemText(listview,iSelect,3,context,4024);	
					  ListView_GetItemText(listview,iSelect,1,title,450);
					  LvItem.pszText=title;
					  Selected=(HTREEITEM)LvItem.lParam;
					  TreeView_SelectItem(treecontrol,Selected);

					  Edit_SetText(editcontrol,LvItem.pszText);
					 // Edit_SetText(textbox,context);
					 // SendMessage(textbox,EM_SETTEXTEX, (WPARAM)&Set,(LPARAM)context);
					  settext(NULL,title);
					  
				}
				if(((LPNMHDR)lParam)->code == NM_RCLICK)
				{
					DelNode(hWnd,1);
				}
					break;
			case ID_TREEVIEW:
				
				if(((LPNMHDR)lParam)->code == NM_DBLCLK) 
				{					
					TCHAR Text[255];
					 TCHAR title[450];
					  TCHAR context[4024];
					memset(&tvi1,0,sizeof(tvi1));
					Selected=(HTREEITEM)SendDlgItemMessage(hWnd,ID_TREEVIEW,TVM_GETNEXTITEM,TVGN_CARET,(LPARAM)Selected);				
					if(Selected==NULL)
					{
						MessageBox(hWnd,_T("TreeView"),_T("Error"),MB_OK|MB_ICONINFORMATION);
						break;
					}				
					TreeView_EnsureVisible(hWnd,Selected);
					SendDlgItemMessage(hWnd,ID_TREEVIEW,TVM_SELECTITEM,TVGN_CARET,(LPARAM)Selected);
					flagSelected=true;
					tvi1.mask=TVIF_TEXT | TVIF_IMAGE 
               | TVIF_SELECTEDIMAGE | TVIF_PARAM;
					tvi1.pszText=Text;
					tvi1.cchTextMax=256;
					tvi1.hItem=Selected;
					TreeView_GetItem(treecontrol,&tvi1);
					
					ListView_SetItemState(listview,count-1-tvi1.lParam,LVIS_FOCUSED|LVIS_SELECTED,LVIS_FOCUSED|LVIS_SELECTED );
					ListView_GetItemText(listview,count-1-tvi1.lParam,3,context,4024);	
					ListView_GetItemText(listview,count-1-tvi1.lParam,1,title,450);

					Edit_SetText(editcontrol,title);
					settext(NULL,title);
					//SendMessage(textbox,EM_SETTEXTEX, (WPARAM)&Set,(LPARAM)context);
					//Edit_SetText(textbox,context);
					

					indexnote=count-1-tvi1.lParam;
				}
				if(((LPNMHDR)lParam)->code == NM_RCLICK)
				{
					DelNode(hWnd,0);
				}
				break;
		}
		break;
	case WM_SIZING:
		//MessageBox(hWnd,_T("Resize"),_T("Error"),MB_OK|MB_ICONINFORMATION);
		RECT rect;
		int width;
		int height;
		if(GetWindowRect(hWnd, &rect))
		{
		  width = rect.right - rect.left;//1025
		  height = rect.bottom - rect.top;//526
		}		
		//tree : 300-30
		if(width>=1025 ||height>=526)
		{
			SetWindowPos(treecontrol,HWND_TOP,0                     ,0, width*0.25,height*0.85,SWP_SHOWWINDOW);
			SetWindowPos(listview,HWND_TOP,   width*0.25            ,0, width*0.45,height*0.85,SWP_SHOWWINDOW);
			SetWindowPos(editcontrol,HWND_TOP,width*0.25+width*0.45 ,0, width*0.3,30,SWP_SHOWWINDOW);
			SetWindowPos(groupbox,HWND_TOP,    width*0.25+width*0.45 ,30,width*0.3,height*0.85-80,SWP_SHOWWINDOW);
			SetWindowPos(toolbar,HWND_TOP,    width*0.25+width*0.45 ,30,width*0.3,height*0.85-80,SWP_SHOWWINDOW);
			SetWindowPos(textbox,HWND_TOP,    width*0.25+width*0.45 ,80,width*0.3,height*0.85-80,SWP_SHOWWINDOW);
		}
		break;
	case WM_CHAR:
		case WM_KEYDOWN:
			{
				if (wParam==0x4E)//N
				{
					int ctrl=GetKeyState(VK_LCONTROL);
					if (ctrl&0x8000)
					{
							ThemNote();							
					}
				}
				if (wParam==0x51)//Q
				{
					int ctrl=GetKeyState(VK_LCONTROL);
					if (ctrl&0x8000)
					{
							DestroyWindow(hWnd);						
					}
				}
				break;
			}
			
		break;
	case WM_COMMAND:
		wmId    = LOWORD(wParam);
		wmEvent = HIWORD(wParam);
		// Parse the menu selections:
		switch (wmId)
		{
		case ID_SAVE:
			Save();
			GhiFile(0,NULL);
			break;
		case ID_BOLD:					
			formattext(1);	
				break;
		case ID_ITALIC:
			formattext(2);
				break;
		case ID_UNDER:
			formattext(3);
				break;
		case ID_FILE_EXPORT:
			//GhiFile(1);
			DialogBox(hInst, MAKEINTRESOURCE(ID_SAVEDIALOG), hWnd, SaveDialog);
			break;
		case ID_FILE_IMPORT:
			//DocFile(1);
			DialogBox(hInst, MAKEINTRESOURCE(ID_OPENDIALOG), hWnd, OpenDialog);
			break;
		case IDM_ABOUT:
			DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);	
			break;
		case IDM_EXIT:
			
			DestroyWindow(hWnd);
			break;
		case ID_EDIT_COPY:
			if(focus==0)
				SendMessage(editcontrol,WM_COPY,0,0);
			if(focus==1)
				SendMessage(textbox,WM_COPY,0,0);
			break;
		case ID_EDIT_DELETE:
			if(focus==0)
				SendMessage(editcontrol,WM_CLEAR,0,0);
			if(focus==1)
				SendMessage(textbox,WM_CLEAR,0,0);
			break;
		case ID_EDIT_CUT:
			if(focus==0)
				SendMessage(editcontrol,WM_CUT,0,0);
			if(focus==1)
				SendMessage(textbox,WM_CUT,0,0);
			break;
		case ID_EDIT_PASTE:
			if(focus==0)
				SendMessage(editcontrol,WM_PASTE,0,0);
			if(focus==1)
				SendMessage(textbox,WM_PASTE,0,0);
			break;
		case ID_EDIT_SELECTALL:					
			if(focus==0)
				Edit_SetSel(editcontrol,0,Edit_GetTextLength(editcontrol));
			if(focus==1)
				Edit_SetSel(textbox,0,Edit_GetTextLength(textbox));
			
			break;
		case ID_NOTE_NEWNOTE:
			ThemNote();
			break;
		case ID_EDITCONTROL:
			focus=0;
			switch (wmEvent)
			{
			case EN_CHANGE:
				TCHAR title[450];								
				if(indexnote!=-1 && edit==1)
				{					
					TCHAR MainT[1024]=_T("");
					ListView_GetItemText(listview,indexnote,2,title,450);
					
					wcscat_s(MainT,title);
					wcscat_s(MainT,_T(" - "));
					Edit_GetText(editcontrol,title,450);
					ListView_SetItemText(listview,indexnote,1,title);
					wcscat_s(MainT,title);
					ListView_SetItemText(listview,indexnote,0,MainT);

					TVITEM temp;
					temp.mask=TVIF_TEXT | TVIF_IMAGE 
				  | TVIF_SELECTEDIMAGE | TVIF_PARAM;
					temp.iImage =IconNam; 
					temp.iSelectedImage = IconNote; 
					temp.lParam=count-1;
					temp.pszText=MainT;
					temp.cchTextMax=256;
					temp.hItem=Selected;
					
					TreeView_SetItem(treecontrol,&temp);	
					GhiFile(0,NULL);
				}
				
				break;
			case EN_SETFOCUS:
				edit=1;
				break;
			case EN_KILLFOCUS:
				edit=0;				
				break;
			default:
				break;
			}
			break;
		case ID_TEXTBOX:
			focus=1;
			switch (wmEvent)
			{
			case EN_CHANGE:
					TCHAR context[4024];
					formattext(1);	
					if(indexnote!=-1 && text==1)
					{				
						SendMessage(textbox,EM_GETTEXTEX,(WPARAM)&Get,(LPARAM)context);
						//ListView_SetItemText(listview,indexnote,3,context);						
					}
					
				break;
			case EN_SETFOCUS:
				text=1;
				break;
			case EN_KILLFOCUS:
				text=0;
				
				break;
			}
			
			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
		}
		break;
	case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps);
		// TODO: Add any drawing code here...
		EndPaint(hWnd, &ps);
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}

// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	UNREFERENCED_PARAMETER(lParam);
	switch (message)
	{
	case WM_INITDIALOG:
		return (INT_PTR)TRUE;

	case WM_COMMAND:
		if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
		{
			EndDialog(hDlg, LOWORD(wParam));
			return (INT_PTR)TRUE;
		}
		break;
	}
	return (INT_PTR)FALSE;
}

INT_PTR CALLBACK SaveDialog(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	TCHAR path[1024];
	UNREFERENCED_PARAMETER(lParam);
	switch (message)
	{
	case WM_INITDIALOG:
		return (INT_PTR)TRUE;

	case WM_COMMAND:
		if ( LOWORD(wParam) == IDCANCEL)
		{
			EndDialog(hDlg, LOWORD(wParam));
			return (INT_PTR)TRUE;
		}
		if(LOWORD(wParam) == IDOK )
		{
			//Edit_GetText(ID_EDITSAVEDIALOG,path,1024);
			GetDlgItemText ( hDlg , ID_EDITSAVEDIALOG , path ,1024 ); 
			GhiFile(1,path);
			EndDialog(hDlg, LOWORD(wParam));
			return (INT_PTR)TRUE;
		}
		break;
	}
	return (INT_PTR)FALSE;
}
INT_PTR CALLBACK OpenDialog(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	TCHAR path[1024];
	UNREFERENCED_PARAMETER(lParam);
	switch (message)
	{
	case WM_INITDIALOG:
		return (INT_PTR)TRUE;

	case WM_COMMAND:
		if ( LOWORD(wParam) == IDCANCEL)
		{
			EndDialog(hDlg, LOWORD(wParam));
			return (INT_PTR)TRUE;
		}
		if(LOWORD(wParam) == IDOK )
		{
			//Edit_GetText(ID_EDITSAVEDIALOG,path,1024);
			GetDlgItemText ( hDlg , ID_EDITOPENDIALOG , path ,1024 ); 
			DocFile(1,path);
			EndDialog(hDlg, LOWORD(wParam));
			return (INT_PTR)TRUE;
		}
		break;
	}
	return (INT_PTR)FALSE;
}